'''
    功能加密压缩文件
'''

folder_path = r"C:\Users\KS00620\Desktop\EMC前期准备照片"
import os, zipfile, pyzipper  # 导入pyzipper模块用于加密zip文件

# with pyzipper.AESZipFile('result.zip', 'w', compression=zipfile.ZIP_DEFLATED) as zipf:
#     zipf.setpassword(b'password123')  # 设置加密密码
#     zipf.write(path)  # 添加文件到zip中
#     print(f"为文件设置密码: password123")  # 输出提示信息

def zip_folder(folder_path, zip_path, password):
    with pyzipper.AESZipFile(zip_path, 'w', compression=pyzipper.ZIP_DEFLATED, encryption=pyzipper.WZ_AES) as zf:
        # 将密码转换为字节类型
        password_bytes = password.encode('utf-8')
        zf.setpassword(password_bytes)
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                abs_path = os.path.join(root, file)
                rel_path = os.path.relpath(abs_path, os.path.dirname(folder_path))
                zf.write(abs_path, rel_path)

zip_folder(folder_path, 'result.zip', '141747')